// 密码找回
(function() {
    //构造函数
    var _con = function(info) {
        var self = this;

        //ID编号
        do {
            //6位数字的id编号
            var _id = /*db_id_prefix + */ Math.random().toString().substr(2, 2) + (+new Date()).toString().substr(-4);
        } while (db.find_by_id(db_table_name, _id));

        self._id = _id;
        self.type = info.type;
        self.people_id = info.people_id;
        self.email = info.email;

        self.create_time = +new Date;
        db.insert(db_table_name, self);

        //超时自动删除
        setTimeout(function() {
            db.remove(db_table_name, _id)
        }, config.findPawTimeOut);

        console.log("创建了一个密码找回：", info.type, _id);
    };
    //PS:模块的暴露放在require skill前面，因为二者互相依赖
    module.exports = _con;
    //技能
    _con.prototype = require("./skill");
    _con.STATE = {
        "未申请兑换": 0,
        "申请兑换中": 1,
        "商家已付款": 2,
        "用户确认收款": 0 //==未申请兑换
    };
    //静态方法
    _con.get = function(Id) {
        var result = db.find_by_id(db_table_name, Id);
        return _con.getInstance(result);
    };
    _con.getInstance = function(obj) {
        if (obj) {
            obj.__proto__ = _con.prototype;
            return obj;
        }
    }
    _con.build = function(type, user_id, email) {
        var info = {
            type: type,
            people_id: user_id,
            email: email
        };
        var self = db.find_one(db_table_name, info);
        if (self) {
            self = _con.getInstance(self);
        } else {
            self = new _con(info);
        }
        return self;
    };
    _con.buildByEmail = function(type, email) {
        if (type === "user") {
            var finder_db_name = config.dbname.user;
            var type_name = 　"用户"
        } else if (type === "bus") {
            var finder_db_name = config.dbname.bus
            var type_name = 　"商家"
        } else {
            throwCusError("密码找回指定类型错误");
        }
        var maybe_people = db.find_one(finder_db_name, {
            info: {
                email: email
            }
        });
        if (!maybe_people) {
            throwCusError("找不到对应邮箱的" + type_name)
        }
        return _con.build(type, maybe_people._id, email);
    };
    _con.buildByPhone = function(type, mobile_phone) {
        if (type === "user") {
            var finder_db_name = config.dbname.user;
            var type_name = 　"用户"
        } else if (type === "bus") {
            var finder_db_name = config.dbname.bus
            var type_name = 　"商家"
        } else {
            throwCusError("密码找回指定类型错误");
        }
        var maybe_people = db.find_one(finder_db_name, {
            info: {
                mobile_phone: mobile_phone
            }
        });
        if (!maybe_people) {
            throwCusError("找不到对应手机号的" + type_name)
        }
        return _con.build(type, maybe_people._id, mobile_phone);
    };

    var db = require("../db/index");
    var config = require("../_include/config");
    var db_table_name = config.dbname.findPaw;
    var db_id_prefix = config.dbIdPrefix.findPaw;
    var BusCon = require("../bus/_con");
    var GoodsCon = require("../goods/_con");
    var helper = require("../_include/helper");

    //服务器重启会丢失setTimeout任务，重启删除过期数据
    db.find_all(db_table_name).slice().forEach(function(find_paw_data) {
        if (find_paw_data) {
            if ((find_paw_data.create_time + config.findPawTimeOut) < +new Date) {
                db.remove(db_table_name, find_paw_data._id);
            }
        }
    });
}());
